package top.jamsee.controller;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.jamsee.entity.Buy_data;
import top.jamsee.entity.ResultInfo;
import top.jamsee.entity.User;
import top.jamsee.exception.NameException;
import top.jamsee.exception.UserException;
import top.jamsee.service.BuyService;
import top.jamsee.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jams
 */
@RequestMapping(value = "/buy")
@Controller
public class BuyController extends BaseController{
    @Resource
    UserService service;
    //所有页数
    int allPages=0;
    @Resource
    BuyService buyService;


    //登录区域
    @RequestMapping(value = "/login.do",method = RequestMethod.POST)
    public ModelAndView page1(String username, String password, HttpServletRequest req, HttpServletResponse resp) throws UserException, IOException {
        User u=new User();
        u.setUsername(username);
        u.setPassword(password);
        int res=service.selectUser(u);
        ModelAndView mv=new ModelAndView();
        if (res==1){
            System.out.println("succssed");
            mv.setViewName("forward:/WEB-INF/view/buy.html");
            HttpSession session=req.getSession();
            session.setAttribute("user",u);



        }else{
            mv.setViewName("view/error");
            throw new NameException("用户名不正确");
        }
        return mv;


    }
    //注销登录
    @RequestMapping(value = "/loginOutUser.do")
    @ResponseBody
    public ResultInfo loginOutUser(){
        ResultInfo res=new ResultInfo();
        try {
            session.removeAttribute("user");
        }catch (Exception ex){
            res.setSuccess(false);
        }

        res.setSuccess(true);
        return res;
    }


    /**
     *
     *  得到用户名
     *
     **/

    @RequestMapping(value = "/getUserName.do")
    @ResponseBody
    public ResultInfo getUserName(){
        ResultInfo res=new ResultInfo();
        User u=getUserFromSession();
        res.setMessage(u.getUsername());
        return res;
    }



    //跳转到买房页面的接口
    @RequestMapping(value = "/buyBlank.do",method = RequestMethod.GET)
    public ModelAndView buy_blank(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("buy");
        return mv;
    }
    /**
     * 跳转到租房页面的接口
     *
     **/
    @RequestMapping(value = "/lentBlank.do",method = RequestMethod.GET)
    public ModelAndView lent_blank(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("forward:/WEB-INF/view/lent.html");

        return mv;
    }
    //跳转到买房租房登记页面的接口
    @RequestMapping(value = "/buyLentRegBlank.do",method = RequestMethod.GET)
    public ModelAndView buyLentReg_blank(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("buyAndLentRegister");
        return mv;
    }




    /**
    * 处理买房的数据响应ajax
    *
    * */
    @RequestMapping(value = "/buyPage.do")
    @ResponseBody
    public List<Buy_data> buyPage(HttpServletRequest req,HttpServletResponse resp,int pageNum){
        //如果用户没有登录则不给他数据
        if (session.getAttribute("user")==null){
            return new ArrayList<Buy_data>();
        }
            //获取一个 ls对象,里面包括 数据信息和分页的总数
            List<List> ls=buyService.SelectBuyData(pageNum);
            List<Buy_data> buy_data=ls.get(0);
            List pageList=ls.get(1);
            //总页数
            allPages = (Integer) pageList.get(0);
            return buy_data;




    }
    //返回总的页数
    @RequestMapping(value = "/allPage.do")
    @ResponseBody
    public int allPage(HttpServletRequest req,HttpServletResponse resp){
        return buyService.selectBuyNum();
    }

    //根据编号删除数据
    @RequestMapping(value = "/buyDel.do",method = RequestMethod.POST)
    @ResponseBody
    public int buyDel(HttpServletRequest req,HttpServletResponse resp,int id){
        //如果用户没有登录则不给他数据
        if (session.getAttribute("user")==null) {
            //没有权限删除数据
            return 0;
        }
            int res = buyService.deleteBuyData(id);
            return res;





    }
    //添加买房表的记录

    @RequestMapping(value = "/addBuy.do")
    @ResponseBody
    public ResultInfo buyAdd(Buy_data buyData, HttpServletRequest request){
        //在作用域中得到用户
        String user="user";
        if (session.getAttribute(user)==null) {
            //没有权限添加
            return new ResultInfo(false,"请登录用户");
        }

            //获取当前系统时间
            buyData.setInsert_time(new Date());
            // new Date()为获取当前系统时间
            ResultInfo res=buyService.addBuyData(buyData,request);

            return res;



    }
    /*
    * 模糊查询buy表
    * @part :传入部分的字符串
    * @pageNum: 传入的页数
    *
    *
    * */
    @RequestMapping(value = "/partBuy.do")
    @ResponseBody
    public List<Buy_data> partBuy(String part,int pageNum){
        //没有权限模糊查询
        if (session.getAttribute("user")==null) {
            return new ArrayList<Buy_data>();
        }

            //获取一个 ls对象,里面包括 数据信息和分页的总数
            List ls = buyService.selectBuyByData(pageNum, part);
            List<Buy_data> buy_data = (List<Buy_data>) ls.get(0);
            //总页数
            allPages = (Integer) ls.get(1);
            return buy_data;





    }
    //返回总的页数(当模糊查询时)
    @RequestMapping(value = "/allPageSelectWhenSearch.do")
    @ResponseBody
    public int allPageSelectWhenSearch(String value){

        return buyService.selectBuyByDataNum(value);
    }
    //返回总的页数(当查询时)
    @RequestMapping(value = "/allPageSelect.do")
    @ResponseBody
    public int allPageSelect(){
        return allPages;
    }
    //返回总的页数(当查询时)
    @RequestMapping(value = "/updateBuy.do",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo updateBuy(int updateId,Buy_data buy_data,HttpServletRequest request){
        //更新插入时间
        buy_data.setInsert_time(new Date());
        buy_data.setId(updateId);
        if (session.getAttribute("user")==null) {
            return new ResultInfo(false,"没有权限更新数据");
        }

        //搜索不到该记录S
        if (buyService.selectBuyById(updateId)==0){
            return new ResultInfo(false,"搜索不到该记录");
        }

            ResultInfo upDateRes=buyService.upDateBuyById(buy_data,request);

            return upDateRes;
    }

}




